package perso.marcr.lst.service.impl;

import java.io.IOException;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.springframework.oxm.XmlMappingException;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import perso.marcr.lst.model.Configuration;
import perso.marcr.lst.param.GlobalParameters;
import perso.marcr.lst.service.ConfigurationService;
import perso.marcr.lst.util.Xml;

@Service
public class ConfigurationServiceImpl implements ConfigurationService {

	public static final String CONFIGURATION_FILE = "config.xml";

	/* (non-Javadoc)
	 * @see perso.marcr.lst.service.ConfigurationService#save(java.lang.String, perso.marcr.lst.model.Configuration)
	 */
	public void save(String jobName, Configuration configuration) throws ParserConfigurationException, TransformerException, XmlMappingException, JAXBException, IOException
	{
		String szFile = GlobalParameters.getJobDirectory(jobName)+"/"+CONFIGURATION_FILE;
		Xml.save(configuration, Configuration.class, szFile);
	}

	/* (non-Javadoc)
	 * @see perso.marcr.lst.service.ConfigurationService#load(java.lang.String)
	 */
	public Configuration load(String jobName) throws ParserConfigurationException, SAXException, IOException, JAXBException
	{	
		String szFile = GlobalParameters.getJobDirectory(jobName)+"/"+CONFIGURATION_FILE;
		return (Configuration) Xml.read(Configuration.class, szFile);
	}	

	/* (non-Javadoc)
	 * @see perso.marcr.lst.service.ConfigurationService#create(java.lang.String, java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see perso.marcr.lst.service.ConfigurationService#create(java.lang.String, java.lang.String)
	 */
	public void create(String jobName, String hostDirectory) throws ParserConfigurationException, TransformerException, XmlMappingException, JAXBException, IOException {
		Configuration configuration = new Configuration();
		configuration.setJobName(jobName);
		configuration.setHostDirectory(hostDirectory);
		save(jobName, configuration);
	}
}
